--
-- Author: dbliu
-- Date: 2014-02-16 12:43:29
--


local PlayerController = import("..entity.wx.ECPlayer")



local EnemyManagerController = import("..entity.wx.ECEntityMgr")

local MapLayer = import("..entity.wx.MapLayer")


local WXPlaneScene = class("WXPlaneScene", function()
    return display.newScene("WXPlaneScene")
end)

function WXPlaneScene:ctor()

	display.addSpriteFramesWithFile("spritesheets/gameArts-hd.plist","spritesheets/gameArts-hd.png")
     
    --大招按钮

    local image_normal = display.newColorLayer(ccc4(255, 255, 255 , 0))
    image_normal:setContentSize(CCSize(200,100))

    local sp_normal = display.newSprite("#bomb.png", 50, 50):addTo(image_normal)

    self.label_bomb_num=ui.newBMFontLabel({
        text = "X 3",
        font = "font/num.fnt",
        x=150,
        y=50,
    }):addTo(image_normal)


    local image_selected = display.newColorLayer(ccc4(255, 255, 255 , 30))
    image_selected:setContentSize(CCSize(200,100))


    self.button_bomb = ui.newImageMenuItem({
        x = display.left+100,
        y = display.bottom +50,
        image = image_normal,
        imageSelected = image_selected,
        listener = handler(self, self.onButtonBombClicked)
    })


    --暂停按钮
    self.button_pause = ui.newImageMenuItem({
        x = display.left + 50 ,
        y = display.top - 50 ,
        image = "#game_pause.png",
        imageSelected = "#game_pause.png" ,
        listener = handler(self, self.onButtonPauseClicked)
    })

 
    -- 创建菜单并加入场景，否则菜单项不会工作
    local menu = ui.newMenu({self.button_bomb , self.button_pause})
    self:addChild(menu)

    -- 分数的label
    self.label_score = ui.newBMFontLabel({
        text = "0000",
        font = "font/num.fnt",
        x = display.left + 100 ,
        y = display.top - 50 ,
        align=ui.TEXT_ALIGN_LEFT,
    }):addTo(self)

    audio.playMusic("music/game_music.mp3")

    self.label=ui.newTTFLabel({text = "you lost", size = 64, align = ui.TEXT_ALIGN_CENTER})
        :pos(display.cx, display.cy)
        :addTo(self,1)
        :hide()

    


    self.maplayer = MapLayer.new("#background_2.png"):addTo(self, -1)
    app.maplayer = self.maplayer
    

    app.world:addCollisionScriptListener(handler(self, self.onCollision_1_2), 1, 2)
    app.world:addCollisionScriptListener(handler(self, self.onCollision_2_6), 2, 2+4)
    app.world:addCollisionScriptListener(handler(self, self.onCollision_2_2), 2, 2)
    app.world:addCollisionScriptListener(handler(self, self.onCollision_1_8), 1, 8)

    app.flightDistance=0
    


    self.player=PlayerController.new(display.cx,display.bottom+display.height/5):addTo(app.maplayer,2)

    self.enemyManager=EnemyManagerController.new():addTo(self)

    self:updateBombLabel()

    -- 注册帧事件
    self:addScriptEventListener(cc.Event.ENTER_FRAME, handler(self, self.tick))

end

function WXPlaneScene:onEnter()
    if device.platform == "android" then
        -- avoid unmeant back
        self:performWithDelay(function()
            -- keypad layer, for android
            local layer = display.newLayer()
            layer:addKeypadEventListener(function(event)
                if event == "back" then app.exit() end
            end)
            self:addChild(layer)

            layer:setKeypadEnabled(true)
        end, 0.5)
    end

    app.world:start()



end



function WXPlaneScene:tick( dt )

    local speed = 10 

    local x,y = app.maplayer:getPosition()
    app.maplayer:setPosition(ccp(x,y-speed))

    if self.player then
        local p = self.player.v:getPlanePosition();
        self.player.v:setPlanePosition(ccp(p.x,p.y+speed))
    end
    

    app.flightDistance=app.flightDistance+speed

    local str_score = string.format("%04d", app.score)
    self.label_score:setString(str_score)


end

function WXPlaneScene:onCollision_1_2( eventType , event )
    if  event:isFirstContact() then

        if eventType=="begin" then
            --删除敌机
            local sprite_ = event:getBody2():getNode()
            local body_ =event:getBody2()
            local view_ = sprite_:getParent()
            local controller_ = view_:getParent()

            controller_:removeSelf(true)
            body_:removeSelf(true)

            --删除玩家
            local sprite_ = event:getBody1():getNode()
            local body_ =event:getBody1()
            local view_ = sprite_:getParent()
            local controller_ = view_:getParent()

            if not controller_.m.fsm__:isState("dead") then
                controller_:be_kill()
                body_:removeSelf(true)
                self.player=nil
            end

            --显示失败提示
            self.label:show()

        end

        

    end
    return true
end

function WXPlaneScene:onCollision_2_6( eventType , event )

    

    if  event:isFirstContact() then
        if eventType=="begin" then
            repeat
                --子弹
                local sprite_2 = event:getBody2():getNode()
                if not sprite_2 then break end

                local body_2 =event:getBody2()
                local view_2 = sprite_2:getParent()
                local controller_2 = view_2:getParent()

                local damage = 1 

                if controller_2.model_:getId()=="bullet1" then
                    damage = 1
                elseif controller_2.model_:getId()=="bullet2" then
                    damage = 2 
                end


                --删除敌机
                local sprite_ = event:getBody1():getNode()
                local body_ =event:getBody1()

                if not sprite_ then break end

                local view_ = sprite_:getParent()
                local controller_ = view_:getParent()

                local model_=controller_.model_
                local blood = model_:getBlood()

                if blood-damage <= 0 then
                    --controller_:removeSelf(true)

                    --加分
                    if controller_.model_:getId()=="wx_enemy_1" then
                        app.score=app.score+1000
                    elseif controller_.model_:getId()=="wx_enemy_2" then
                        app.score=app.score+5000
                    elseif controller_.model_:getId()=="wx_enemy_3" then
                        app.score=app.score+10000
                    end
                    

                    controller_:be_kill()
                    body_:removeSelf(true)
                    app:remove_enemy(controller_)
                else
                    blood=blood-damage
                    model_:setBlood(blood)
                    controller_:onFreeze()
                end

                --删除子弹
                

                controller_2:removeSelf(true)
                body_2:removeSelf(true)
            until false
        end

    end
    return true
end

function WXPlaneScene:onCollision_2_2( eventType , event )
    
    
    local body_ =event:getBody1()
    body_:setRotation(0)

    local body_ =event:getBody2()
    body_:setRotation(0)
        
    return true
end

function WXPlaneScene:onCollision_1_8( eventType , event )
    
    
    if  event:isFirstContact() then

        if eventType=="begin" then

            


            --根据道具类型变更玩家属性
            local sprite_2 = event:getBody2():getNode()
            local body_2 =event:getBody2()
            local view_2 = sprite_2:getParent()
            local controller_2 = view_2:getParent()

            if controller_2.model_:getId()=="prop_bullet" then
                --得到玩家controller
                local sprite_ = event:getBody1():getNode()
                local body_ =event:getBody1()
                local view_ = sprite_:getParent()
                local controller_ = view_:getParent()

                controller_:upgrade_bullet()
            end

            if controller_2.model_:getId()=="prop_bomb" then

                --得到玩家controller
                local sprite_ = event:getBody1():getNode()
                local body_ =event:getBody1()
                local view_ = sprite_:getParent()
                local controller_ = view_:getParent()
                
                controller_:upgrade_bomb()
                self:updateBombLabel()
            end

            --删除道具
            controller_2:be_kill()
            --controller_2:removeSelf(true)
            body_2:removeSelf(true)
        end

        

    end
    return true
end


function WXPlaneScene:updateBombLabel()

    self.label_bomb_num:setString("X "..self.player.bomb_num)

    if self.player.bomb_num > 0 then 
        self.button_bomb:setEnabled(true)
        self.button_bomb:setVisible(true)
    else
        self.button_bomb:setEnabled(false)
        self.button_bomb:setVisible(false)
    end
end

function WXPlaneScene:onButtonBombClicked( tag )
    self.player.bomb_num=self.player.bomb_num-1
    self.enemyManager:kill_all()
    self:updateBombLabel()
end

function WXPlaneScene:onButtonPauseClicked( tag )
    if CCDirector:sharedDirector():isPaused() then
        CCDirector:sharedDirector():resume()
    else
        CCDirector:sharedDirector():pause()
    end
    
end

return WXPlaneScene
